Ext.define("app.system.role.form", {
	extend : 'Ext.window.Window',
	title : '编辑角色信息',
	height : 600,
	width : 800,
	closable : true,
	resizable : false,
	modal : true,
	constrain : true,
	plain : true,
	layout : {
		type : 'border'
	},
	requires : [ 'app.system.menu.tree' ],
	initComponent : function() {
		var me = this;
		me.form = me.createForm();
		Ext.apply(me, {
			items : [ me.form ],
			buttons : [ {
				text : '确认',
				bodyStyle : 'padding:0 0 0 5px',
				handler : function() {
					me.submitForm();
				}
			}, {
				text : '关闭',
				bodyStyle : 'padding:0 0 0 5px',
				handler : function() {
					me.close();
				}
			} ]
		});
		me.callParent(arguments);
	},
	submitForm : function() {
		var me = this;
		var account = utils.getAccountField('accountType');
		if (account != 0 && account != 1) {
			Ext.Msg.alert('提示', '没有权限');
			return;
		}
		var params = {};
		var data1s = me.tree1.getCheckedRecord();
		if (data1s.length == 0) {
			Ext.Msg.alert('提示', '权限没有选择');
			return;
		}
		params['authority'] = me.converToString(data1s);
		if (account == 0) {
			var data2s = me.tree2.getCheckedRecord();
			if (data2s.length == 0) {
				Ext.Msg.alert('提示', '菜单没有设置');
				return;
			}
			params['menu'] = me.converToString(data2s);
		}
		if (me.roleId) {
			params['roleId'] = me.roleId;
		}
		evt.submit(me.form, systemUrl + '/boyon/role/editor', params,
				function() {
					Ext.Msg.alert('提示', '操作成功');
					me.parentTarget.reloadStore();
					me.close();
				});
	},
	loadData : function(form) {
		var me = this;
		var roleId = me.roleId;
		if (!roleId) {
			return;
		}
		utils.loadform({
			form : me,
			model : 'app.system.model.role',
			url : systemUrl + '/boyon/role/find',
			extraParams : {
				'roleId' : me.roleId
			},
			fn : function(rec) {
				me.form.getForm().loadRecord(rec);
			}
		});
	},
	createForm : function() {
		var me = this;
		var fields = me.createField();
		var form = Ext.create('Ext.form.Panel', {
			layout : 'column',
			region : 'center',
			items : fields,
			listeners : {
				afterrender : function() {
					me.loadData(form);
				}
			}
		});
		return form;
	},
	createField : function() {
		var me = this;
		var array = new Array();
		utils.pushfieldarray(array, '1', {
			name : 'roleName',
			beforeLabelTextTpl : required,
			fieldLabel : '角色名称',
			allowBlank : false,
			columnWidth : .5,
			minLength : 1,
			maxLength : 16
		});
		utils.pushfieldarray(array, 'app.widget.value', {
			name : 'roleType',
			beforeLabelTextTpl : required,
			fieldLabel : '角色类型',
			valueField : 'value',
			displayField : 'displayName',
			valueType : 6,
			columnWidth : .5,
			parentTarget : me,
			func : me.handlerAccountType,
			editable : false,
			allowBlank : false
		});
		utils.pushfieldarray(array, '6', {
			name : 'summary',
			fieldLabel : '概述',
			columnWidth : 1,
			maxLength : 64
		});
		utils.pushfieldarray(array, '6', {
			name : 'remark',
			fieldLabel : '备注',
			columnWidth : 1,
			maxLength : 64
		});
		var accountType = utils.getAccountField('accountType');
		if (accountType == 0) {
			me.rolePanel(array);
			me.managerPanel(array);
		} else if (accountType == 1) {
			me.rolePanel(array);
		}
		return array;
	},
	handlerAccountType : function(array) {
		var accountType = utils.getAccountField('accountType');
		var newArray = new Array();
		for (var i = 0; i < array.length; i++) {
			var obj = array[i];
			if (accountType == 0) {
				if (obj.value == 1) {
					newArray.push(obj);
				}
			} else if (accountType == 1) {
				if (obj.value == 2) {
					newArray.push(obj);
				}
			}
		}
		return newArray;
	},
	rolePanel : function(array) {
		var me = this;
		me.tree1 = Ext.create('app.system.menu.tree', {
			title : '权限设置',
			columnWidth : .5,
			height : 150,
			style : 'margin: 5px 5px 5px 5px',
			url : systemUrl + '/boyon/role/find/authority',
			extraParams : {
				roleId : me.roleId
			}
		});
		array.push(me.tree1);
	},
	managerPanel : function(array) {
		var me = this;
		me.tree2 = Ext.create('app.system.menu.tree', {
			title : '菜单设置',
			columnWidth : .5,
			height : 150,
			style : 'margin: 5px 5px 5px 5px',
			url : systemUrl + '/boyon/role/find/menu',
			extraParams : {
				roleId : me.roleId
			}
		});
		array.push(me.tree2);
	},
	converToString : function(datas) {
		var ids = null;
		for (var i = 0; i < datas.length; i++) {
			var o = datas[i];
			ids = ids == null ? o.menuId : ids + ',' + o.menuId;
		}
		return ids;
	}
});